// Do file to do the analysis presented in Table A6 in the BJPS paper
//
// Last changed: 2020-04-01 (KOL)

 set matsize 10000
 
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  log using "`output'\Output\TableA4b.log", replace text
  
  use "E:\ProjData\UtplaceringsData8594\utp-UtpData.dta", clear 
  
  merge 1:1 LopNr Ar using "E:\ProjData\UtplaceringsData8594\utp-EthnicContacts.dta"
  replace ArbAnd = 0 if ArbAnd == .
  
  replace mNom = mNom*100
  replace mVald = mVald*100
  
  rename mNom Nominerad
  rename mVald Vald
  destring sKommun, gen(KommunValAr) 
  
//Keep only refugee immigrants
  keep if FlyktInv == 1
    
//Recode earnings to monthly wages in 1000 SEK
  replace LoneInk = ((LoneInk*100)/12)/1000

  gen Kvinna = -1*(Kon-1)
  
 // Merge on municipality data 
   gen kkod = KommunKod
   
   capture drop _merge
   merge m:1 kkod Ar using "E:\ProjData\UtplaceringsData8594\utp-MunData.dta"
   
  listwise Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr TidI InvAlder logpEnklavVuxenInvAr ///
           mUtb_infodda Herfindahl effectiveparties disproportionality //leftshare
 
  gen invHerfindahl = 1-Herfindahl
  
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  local controls4 "effectiveparties disproportionality leftshare migrantshare invHerfindahl mUtb_infodda"

  egen InKomByKom = group(InKommun KommunValAr)
  
  qui: reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(KommunValAr) stages(first) old 
  keep if e(sample) == 1
  
//################################ Column 1 of Table A4b ###########################// 
  
  reghdfe Nominerad `controls2' RMKvot (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo c1
  
//################################ Column 2 of Table A4b ###########################// 
  
  reghdfe Nominerad `controls2' RMKvot `controls4' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', ///
                                 absorb(InKommun `controls1') cluster(InKommun) stages(first) old keepsingletons
  eststo c2
  
//################################ Column 3 of Table A4b ###########################// 
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', ///
                                 absorb(InKommun `controls1' KommunValAr) cluster(InKommun KommunValAr) stages(first) old 
  eststo c3
 

 //################################ Column 4 of Table A4b ###########################// 
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', ///
                                 absorb(InKomByKom `controls1') cluster(InKomByKom) stages(first) old keepsingletons
  eststo c4
 
  reghdfe Nominerad `controls2' `controls4' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', ///
                                 absorb(`controls1') cluster(InKommun) stages(first) old 
  eststo c5
  
  esttab c1 c2 c3 c4 c5 using "`output'\Output\TableA6.rtf", keep(RMKvot `controls4' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
 
log close 
	